import Compiler from './compiler';
import Observer from './observer';

export default class Vue {
  constructor(options) {
    this.$options = options || {};
    this.$data = options.data || {};
    this.$methods = options.methods || {};
    this.$el =
      typeof options.el === 'string' ? document.querySelector(options.el) : el;
    this._proxyData(options.data);
    this._proxyMethods(options.methods);
    new Observer(options.data);
    new Compiler(this);
  }
  _proxyData(data) {
    for (const key in data) {
      if (data.hasOwnProperty(key)) {
        Reflect.defineProperty(this, key, {
          enumerable: true,
          configurable: true,
          get() {
            return data[key];
          },
          set(newValue) {
            if (newValue !== data[key]) {
              data[key] = newValue;
            }
          },
        });
      }
    }
  }
  _proxyMethods(methods) {
    for (const key in methods) {
      if (methods.hasOwnProperty(key)) {
        Reflect.defineProperty(this, key, {
          enumerable: true,
          configurable: true,
          get() {
            return methods[key];
          },
          set(newValue) {
            if (newValue !== methods[key]) {
              methods[key] = newValue;
            }
          },
        });
      }
    }
  }
}
